package QQChat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.net.Socket;
import java.util.Properties;

/**
 * @Description:客户端简单实现
 * @Author:AMark
 * @Date: 11/4/2023
 */
public class ClientChatMain extends JFrame implements ActionListener, KeyListener {
    public static void main(String[] args) {
        new ClientChatMain();
    }
    private JTextArea jta;
    private JScrollPane jsp;
    private JPanel jp;
    private JTextField jtf;
    private JButton jb;
    //客户端Ip地址
    private static String clientIp;
    private static int clientPort;

    //静态代码块加载外部配置文件
    static {
        Properties prop = new Properties();
        try {
            prop.load(new FileReader("QQChat/chat.properties"));
            clientIp = prop.getProperty("clientIp");
            clientPort = Integer.parseInt(prop.getProperty("clientPort"));
        } catch (FileNotFoundException e) {
            System.out.printf("客户端文件没找到！");
        } catch (IOException e) {
            System.out.printf("客户端文件读取有误!");
        }
    }
    //数据输入流
    BufferedWriter bw;

    public ClientChatMain() {
        jta = new JTextArea();
        //设置文本域不可编辑
        jta.setEditable(false);
        //将文本向前到滚动条中，实现滚动效果
        jsp = new JScrollPane(jta);
        jp = new JPanel();
        jtf = new JTextField(10);
        jb = new JButton("发送");
        jp.add(jtf);
        jp.add(jb);

        //添加组件
        this.add(jsp, BorderLayout.CENTER);
        this.add(jp, BorderLayout.SOUTH);

        //设置标题、大小、位置
        this.setTitle("QQ聊天客户端");
        this.setSize(300, 300);
        this.setLocation(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        /*********TCP服务端start*****************/
        jb.addActionListener(this);
        jtf.addKeyListener(this);
        //添加键盘方法
        //1、创建一个客户端的套接字(尝试连接)
        try {
            Socket socket = new Socket(clientIp,clientPort);

        //2、获取socket通道的输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));


        //3、获取socket通道的输出流
        bw = new BufferedWriter((new BufferedWriter((new OutputStreamWriter(socket.getOutputStream())))));
        //循环读取数据并拼接到文本域中
        String line = null;
        while ((line = br.readLine())!=null) {
            jta.append(line + System.lineSeparator());
        }

        //4、关闭socket通道
        socket.close();
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
        /*****************TCP服务器end***************/
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        sendDataToSocket();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            System.out.printf("jjjjj");
            sendDataToSocket();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
    //定义方法将数据发送到socket通道
    private void sendDataToSocket() {
        System.out.println("发送按钮被点击了...");
        //获取文本框中发送的内容
        String text = jtf.getText();
        //拼接需要发送的数据内容
        text = "服务端对客户端说：" + text + System.lineSeparator();
        //自己也需要显示
        jta.append(text);
        try {
            //发送数据
            bw.write(text);
            bw.flush();
            //清空文本框内容
            jtf.setText("");
        }catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
